package com.tsf.demo.consumer.controller;

import com.tsf.demo.commons.vo.Product;
import com.tsf.demo.consumer.proxy.InventoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
public class OrderController {

	//最大库存
	@Value("${maxInventory:100}")
	private int maxInventory;

	@Autowired
	private InventoryService inventoryService;

	//查询库存
	@RequestMapping(value = "/product/{id}", method = RequestMethod.GET)
	public Product getProduct(@PathVariable String id) {
		return inventoryService.getProduct(id);
	}

	//销售
	@RequestMapping(value = "/order/sales", method = RequestMethod.GET)
	public String sales(@RequestParam(value="productId", required=true) String productId,
						  @RequestParam(value="quantity", required=true) Integer quantity) {
		if (quantity>maxInventory)
		{
			return "403 超过库存上限";
		}
		return inventoryService.outbound(productId,quantity);
	}

	//采购
	@RequestMapping(value = "/order/supply", method = RequestMethod.GET)
	public String supply(@RequestParam(value="productId", required=true) String productId,
						  @RequestParam(value="quantity", required=true) Integer quantity) {
		if (quantity>maxInventory)
		{
			return "403 超过库存上限";
		}
		return inventoryService.inbound(productId,quantity);
	}

}